#include "stdafx.h"

extern WSASetLastError_proxy_t pTrueWSASetLastError;

int shutdown_proxy ( 
    __in SOCKET s,
    __in int    how
    )
{
    try
    {
    //SEND
        Header header(GetCurrentThreadId(), "shutdown");
        shutdown_in_inout shutdownIN(s, how);
        
        std::vector<char> out_buffer;
        Serialize(header, shutdownIN, out_buffer);

        SendToClient (out_buffer);
        
    //RECEIVE()
        shutdown_out_inout shutdownOUT;

        std::vector<char> in_buffer;
        
        RecvFromClient (in_buffer, header);
        Deserialize deserialize(in_buffer);
        deserialize(shutdownOUT);

        if(shutdownOUT.wsa_last_error_ != 0)
        {
            pTrueWSASetLastError(shutdownOUT.wsa_last_error_);
            return SOCKET_ERROR;
        }
        return 0;
    }
    catch (std::exception& ex)
    {
        LOG(plog::error) << "shutdown: " << ex.what();
    }
    pTrueWSASetLastError (WSAEWOULDBLOCK);
    return SOCKET_ERROR;
}
